﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace TRUONG_QUOC_PHIEN_SAI_GLTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string GA = "GA";
        private string VIT = "VIT";
        private string NGAN = "NGAN";
        private Thread thread;

        public static Image temp_Image;
        public static int eggSum;
        public static int level;
        public static int live;

        public MainWindow()
        {
            InitializeComponent();
            mainLoaded();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Right:
                    if (image_Ro.Margin.Right > 0)
                    {
                        image_Ro.Margin = new Thickness(image_Ro.Margin.Left + 10, image_Ro.Margin.Top, image_Ro.Margin.Right - 10, image_Ro.Margin.Bottom);
                        temp_Image.Margin = image_Ro.Margin;
                    }
                    break;

                case Key.Left:
                    if (image_Ro.Margin.Left > 0)
                    {
                        image_Ro.Margin = new Thickness(image_Ro.Margin.Left - 10, image_Ro.Margin.Top, image_Ro.Margin.Right + 10, image_Ro.Margin.Bottom);
                        temp_Image.Margin = image_Ro.Margin;
                    }
                    break;

                case Key.G:
                    GiaCam ga = new GiaCam(GA);
                    mainGrid.Children.Add(ga);
                    mainGrid.Children.Add(ga.sp);
                    ga.thread.Start();
                    break;

                case Key.V:
                    GiaCam vit = new GiaCam(VIT);
                    mainGrid.Children.Add(vit);
                    mainGrid.Children.Add(vit.sp);
                    vit.thread.Start();
                    break;

                case Key.N:
                    GiaCam ngan = new GiaCam(NGAN);
                    mainGrid.Children.Add(ngan);
                    mainGrid.Children.Add(ngan.sp);
                    ngan.thread.Start();
                    break;
            }
            
        }

        private void mainLoaded()
        {
            temp_Image = new Image();
            temp_Image.Margin = image_Ro.Margin;

            eggSum = 0;
            level = 1;
            live = 3;

            GiaCam gc = new GiaCam(GA);
            mainGrid.Children.Add(gc);
            mainGrid.Children.Add(gc.sp);
            gc.thread.Start();

            thread = new Thread(new ThreadStart(runThread));
            thread.Start();
        }

        private void runThread()
        {
            while (true)
            {
                if (live == 0)
                {
                    MessageBox.Show("Hết lượt chơi, Chơi lại từ đầu");
                    eggSum = 0;
                    level = 1;
                    live = 3;
                    System.Diagnostics.Process.Start(App.ResourceAssembly.Location);
                    App.Current.Dispatcher.BeginInvoke
                    (
                    DispatcherPriority.Normal,
                    (ThreadStart)delegate()
                    {
                        App.Current.Shutdown();
                    }
                    );
                }
            }
        }
    }
}
